#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
Introduction to Programming in Java - An Interdisciplinary Approach

All book programs ported to Python by
Scott C. King (revisionx at {gee}ma1l <dot> c0m).

All program examples pass Pylint with a perfect 10.00 score.
Pylint:  http://www.pylint.org/


Exercise 1.4.35 - Birthday problem

Suppose that people enter an empty room until a pair of people share a birthday.
On average, how many people will have to enter before there is a match?
Run experiments to estimate the value of this quantity. Assume birthdays to be
uniform random integers between 0 and 364.


Syntax:
python birthday_problem.py


Notes:
int(sys.argv[1]) == Integer.parseInt(args[0])

sys is a required import here in order to use command line arguments.
https://docs.python.org/2/tutorial/stdlib.html

random is a required import as a substitute for Java's Math.random()
https://docs.python.org/2/library/random.html

Division updated in PEP 238 is better suited for this task, which is
imported using the __future__ module.

https://www.python.org/dev/peps/pep-0238
https://docs.python.org/2.7/library/__future__.html

'''

from __future__ import division
import sys
import random


def main():
    ''' Simulate the probability of repeat birthdays using various
        class sizes and user_defined number of trials '''

    try:
        trials = int(sys.argv[1])
    except IndexError:
        print 'Integer for number of trials is required!'
        print 'Syntax: "python birthday_problem.py 10000"'
        sys.exit(0)

    probability = 1.0
    class_size = [_ for _ in range(2, 60, 2)]

    for a_class in class_size:
        duplicates = 0
        for _ in range(trials):
            days_of_year = [0] * 365

            for _ in range(a_class):
                birthday = random.randrange(365)
                days_of_year[birthday] += 1

                if days_of_year[birthday] > 1:
                    duplicates += 1
                    break

        probability = duplicates / trials
        output = 'For {} trials, the probability of a shared birthday '\
                 'in a class of {} is {}%.'
        print output.format(trials, a_class, probability * 100)


if __name__ == '__main__':
    main()
